#pragma once
#include "GlobalDefinition.h"
#include "DataDefinition.h"
#include "DateTime.h"
#include <map>
#include <string>

using namespace std;

/**  
* Connected client information
*/
typedef struct tagConnection
{
	FileType m_stFileType;			// Client required file type
	string m_strRemoteIP;			// Remote IP address
	unsigned int m_uiRemotePort;	// Remote port
	string m_strLocalIP;			// Local IP address
	unsigned int m_uiLocalPort;	// Local port
	double m_dSendBytes;			// Currently sent bytes
	double m_dRecvBytes;			// Currently received bytes
	unsigned int   m_uiConnectType;			// 0:Server 1:Client
	DateTime m_cLastSendTime;		// Last send time to client

	/**
	* A constructor.
	*/
	tagConnection()
		:m_stFileType()
		,m_strRemoteIP()
		,m_uiRemotePort(0)
		,m_strLocalIP()
		,m_uiLocalPort(0)
		,m_dSendBytes(0)
		,m_dRecvBytes(0)
		,m_uiConnectType(0)
	{
		m_cLastSendTime.SetToCurrentDateTime();
	}
}Connection;


class ConnectionInformation
{
public:
	ConnectionInformation();
	void SetFileType(SOCKET p_stClient,FileType p_stFileType);
	FileType GetFileType(SOCKET p_stClient);
	void SetRemoteIP(SOCKET p_stClient,string p_strIP);
	string GetRemoteIP(SOCKET p_stClient);
	void SetRemotePort(SOCKET p_stClient,unsigned int p_strPort);
	unsigned int GetRemotePort(SOCKET p_stClient);
	void SetLocalIP(SOCKET p_stLocal,string p_strIP);
	void SetLocalPort(SOCKET p_stLocal,unsigned int p_strPort);
	void SetTransBytes(SOCKET p_stClient,double p_dSentBytes,double p_dRecvBytes);
	void GetTransBytes(SOCKET p_stClient,double& p_dSentBytes,double& p_dRecvBytes);
	void SetConnectType(SOCKET p_stConnection,unsigned int p_uiConnectType);
	void SetConnection(SOCKET p_stConnectionSocket, Connection p_stConnection);
	void SetTime(SOCKET p_stClient,DateTime p_cDateTime);
	map<SOCKET,Connection> GetConnectionList();
	void DeleteClient(SOCKET p_stClient);

private:
	bool IsConnectionExist(SOCKET p_stClient, map<SOCKET,Connection>::iterator& p_it);

private:
	CRITICAL_SECTION  m_csLock;
	map<SOCKET,Connection> m_mapConnectionList;
};

